package com.fxsh168.server.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fxsh168.server.R;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.interfaces.Constant;
import com.fxsh168.server.entitys.RegiYZMCG;
import com.fxsh168.server.services.VoiceSynthesisService;
import com.fxsh168.server.utils.MD5Utils;
import com.fxsh168.server.utils.PopInfo;
import com.fxsh168.server.utils.RegExp;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;


public class LoginActivity extends BaseActivity {
    private TextView mForgetPass, mReGin;
    private Button mBTLogin;
    private EditText mPutPass, mPutPhone;
    private RelativeLayout mReturnActivity;
    private String mPhone;
    private String mPassword;
    private String mToken;
    private boolean isFirst = false; //只要进入过主界面，就说明已经不是第一次进入这个应用
    private SharedPreferences sp;
    private static final String FILENAME = "activity_login";
    ImageView animation_ivLogin;
    PopInfo popInfo;
    RelativeLayout reMain;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        sp = getSharedPreferences(FILENAME, MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean("isFirst", false);
        edit.commit();
        //    开启服务，下载音频
        Intent intent = new Intent(LoginActivity.this, VoiceSynthesisService.class);
        startService(intent);
        initUI(); // 控件初始化
    }

    // 控件初始化
    private void initUI() {
        mForgetPass = (TextView) findViewById(R.id.mForgetPass);
        mReGin = (TextView) findViewById(R.id.mReGin);
        mBTLogin = (Button) findViewById(R.id.mBTLogin);
        mPutPass = (EditText) findViewById(R.id.mPutPass);
        mPutPhone = (EditText) findViewById(R.id.mPutPhone);
        mReturnActivity = (RelativeLayout) findViewById(R.id.mReturnActivity);
        reMain = (RelativeLayout) findViewById(R.id.reMain);
        mForgetPass.setOnClickListener(onShowClick);
        mReGin.setOnClickListener(onShowClick);
        mBTLogin.setOnClickListener(onShowClick);
        mPutPass.setOnClickListener(onShowClick);
        mPutPhone.setOnClickListener(onShowClick);
        mReturnActivity.setOnClickListener(onShowClick);
        animation_ivLogin = (ImageView) findViewById(R.id.animation_ivLogin);
    }

    private View.OnClickListener onShowClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.mReturnActivity:
                    finish();
                    break;
                case R.id.mReGin:
                    jumpActivity(ReGinsterActivity.class);
//                    jumpActivity(ReginPhotoActivity.class);
                    break;
                case R.id.mForgetPass:
                    jumpActivity(ForgetPassActivity.class);
                    //找回密码
                    break;
                case R.id.mBTLogin:
                    refreshUi(true, animation_ivLogin);
                    btnLogin();
                    break;
                case R.id.teOk:
                    popInfo.dismiss();
                    break;
                default:
                    break;
            }
        }
    };

    public void btnLogin() {
        mPhone = mPutPhone.getText().toString().trim();
        mPassword = mPutPass.getText().toString().trim();
        if ((!TextUtils.isEmpty(mPhone)) && mPhone != null ) {
            if ((mPassword != null && !TextUtils.isEmpty(mPassword)) && RegExp.checkPassword(mPassword)) {
                String jMPassword = MD5Utils.MD532(mPassword);
                mToken = MD5Utils.MD532("lis" + mPhone + "bin" + jMPassword + "www625kejicom");
                OkHttpUtils.post().addParams("token", mToken).addParams("phone", mPhone)
                        .addParams("password", jMPassword).url(Constant.LOGIN)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                showToast("连接不上服务器!");
                                mPutPhone.getText().clear();
                                mPutPass.getText().clear();
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                if (response != null) {
                                    RegiYZMCG regiYZMCG = new Gson().fromJson(response, RegiYZMCG.class);
                                    String code = regiYZMCG.getCode();
                                    if (code.equals(200 + "")) {
                                        refreshUi(false, animation_ivLogin);
                                        showToast("登陆成功!");
                                        Log.e("Tag", "==登陆==" + response);
                                        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
                                        SharedPreferences.Editor editor = sp.edit();
                                        editor.putString("mPhone", mPhone);
                                        editor.putString("mPassword", mPassword);
                                        editor.putString("token", mToken);
                                        editor.commit();
//                                        startActivityForResult(new Intent(LoginActivity.this, Activity_start.class), Constant.INDEXREQUESTCODE);
                                        jumpActivity(MainActivity.class);
                                    } else if (code.equals(201 + "")) {
                                        refreshUi(false, animation_ivLogin);
                                        showToast("非法请求!");
                                    } else if (code.equals(202 + "")) {
                                        refreshUi(false, animation_ivLogin);
                                        popInfo = new PopInfo(LoginActivity.this, reMain, "账号不存在!", onShowClick);
                                    } else if (code.equals(203 + "")) {
                                        refreshUi(false, animation_ivLogin);
                                        popInfo = new PopInfo(LoginActivity.this, reMain, "密码错误!", onShowClick);
                                    } else if (code.equals(204 + "")) {
                                        refreshUi(false, animation_ivLogin);
                                        popInfo = new PopInfo(LoginActivity.this, reMain, "正在审核中!", onShowClick);
                                    } else if (code.equals(205 + "")) {
                                        refreshUi(false, animation_ivLogin);
                                        popInfo = new PopInfo(LoginActivity.this, reMain, "未通过审核!", onShowClick);
                                    }
                                }
                            }
                        });
            } else {
                mPutPass.setError("请输入正确的6-18位正确的密码!");
            }
        } else {
            mPutPhone.setError("请输入11位正确的手机号!");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (requestCode == Constant.REGISREQUESTCODE && resultCode == Constant.REGISRESPONDCODE) {
                //注册传过来的值
                Bundle regiBundle = data.getBundleExtra("regiBundle");
                String phone1 = regiBundle.getString("phone");
                String password1 = regiBundle.getString("password");
                mPutPhone.setText(phone1);
                mPutPass.setText(password1);
            } else if (requestCode == Constant.RETRREQUESTCODE && resultCode == Constant.RETRRESPONDCODE) {
                //找回密码传过来的值
                Bundle retrBundle = data.getBundleExtra("retrBundle");
                String phone2 = retrBundle.getString("phone");
                String password2 = retrBundle.getString("password");
                mPutPhone.setText(phone2);
                mPutPass.setText(password2);
            } else if (requestCode == Constant.INDEXREQUESTCODE && resultCode == Constant.INDEXRESPONDCODE) {
//                mPutPhoneLogin.getText().clear();//退出时只清除密码不清除账号
//                mPutPasswordLogin.getText().clear();
                //Index传过来的值
                String phone2 = data.getStringExtra("mPhone").trim();
                String password2 = data.getStringExtra("mPassword").trim();
                mPutPhone.setText(phone2);
                mPutPass.setText(password2);
            }
        }
    }


}
